package com.fenxiangle.yueding.feature.mine.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.aliyun.vod.common.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.ToastUtils;
import com.fenxiangle.yueding.common.widget.dialog.BounceTopEnter;
import com.fenxiangle.yueding.common.widget.dialog.NormalDialog;
import com.fenxiangle.yueding.common.widget.dialog.OnBtnClickL;
import com.fenxiangle.yueding.common.widget.dialog.SlideBottomExit;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.TeamBo;
import com.fenxiangle.yueding.feature.focus.view.adapter.DemandManageAdapter;
import com.fenxiangle.yueding.feature.mine.contract.TeamContract;
import com.fenxiangle.yueding.feature.mine.dependencies.team.DaggerTeamComponent;
import com.fenxiangle.yueding.feature.mine.dependencies.team.TeamPresenterModule;
import com.fenxiangle.yueding.feature.mine.view.adapter.MyDemandMagageAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @ProjectName: YueDing
 * @Package: com.fenxiangle.yueding.feature.mine.view
 * @ClassName: MyDemandManageActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/5 16:10
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/5 16:10
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MyDemandManageActivity extends BaseActivity implements OnRefreshListener, TeamContract.View, BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_coupon)
    RecyclerView rvCoupon;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout smartRefesh;
    MyDemandMagageAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<TeamBo> mData;
    private String userUid, orderId;

    @Inject
    TeamContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initInjector() {
        DaggerTeamComponent.builder().teamPresenterModule(new TeamPresenterModule(this)).build().inject(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_demand_manage;
    }


    @Override
    protected void initData() {
        userUid = this.getIntent().getStringExtra("user_id");
        orderId = this.getIntent().getStringExtra("orderId");
        pageIndex = 1;
        mPresenter.getUserList(pageIndex, pageSize, userUid, orderId, 0);

    }

    @Override
    public void showUserStatusSucc(OrderBo orderBo) {
        nowNum += 1;
        pageIndex = 1;
        mPresenter.getUserList(pageIndex, pageSize, userUid, orderId, 0);
    }

    @Override
    public void showSubmitUserSucc(OrderBo orderBo) {
        finish();
    }

    @Override
    public void showTeamListSuccess(List<TeamBo> workBoPageData) {

    }

    TextView num, submit;

    @Override
    protected void initView() {
        super.initView();
        initAdapter();
        initToolBar(toolbar, "需求管理", true, true);
        num = toolbar.findViewById(R.id.tv_demand_manage_num);
        submit = toolbar.findViewById(R.id.tv_demand_manage_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nowNum <= 0) {
                    showMsg("必须选择一个用户");
                    return;
                }
                NormalDialog normalDialog = new NormalDialog(MyDemandManageActivity.this);
                normalDialog.title("")
                        .content("确定形成订单?")//
                        .btnNum(2)
                        .showAnim(new BounceTopEnter())//
                        .dismissAnim(new SlideBottomExit());
                normalDialog.setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        if (normalDialog.isShowing()) {
                            normalDialog.superDismiss();
                        }
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        if (mData != null) {//确定形成订单？
                            mPresenter.submitUser(orderId);
                        }
                        normalDialog.superDismiss();
                    }
                });
                normalDialog.show();

            }
        });
    }

    public void initAdapter() {
        smartRefesh.setEnableLoadMore(false);
        smartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(rvCoupon);
        rvCoupon.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyDemandMagageAdapter();
        mAdapter.bindToRecyclerView(rvCoupon);
        mAdapter.setOnLoadMoreListener(this, rvCoupon);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showUserListSuccess(List<TeamBo> workBoPageData) {
        smartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
        int allNum = mData.get(0).getAllNum();
        nowNum = mData.get(0).getNowNum();
        num.setText(nowNum + "/" + allNum);
    }

    private int nowNum;

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showTeamListError(String error) {
        ToastUtil.showToast(this, error);
    }

    @Override
    public void showTeamListMoreSuccess(List<TeamBo> msg) {

    }

    @Override
    public void showTeamListMoreError(String msg) {

    }

    @Override
    public void showTeamListEmpty() {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mAdapter.setEnableLoadMore(false);
        initData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.tv_level_member:
                if (mData.get(position).getGrabStatus() == 1) {
                    showMsg("选定后请点击右上\n" +
                            "角确认键形成订单");
                    mPresenter.userStatus(orderId, mData.get(position).getUserUid(), 2);
                } else {
                    NormalDialog normalDialog = new NormalDialog(this);
                    normalDialog.title("")
                            .content("确定取消选定?")//
                            .btnNum(2)
                            .showAnim(new BounceTopEnter())//
                            .dismissAnim(new SlideBottomExit());
                    normalDialog.setOnBtnClickL(new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            if (normalDialog.isShowing()) {
                                normalDialog.superDismiss();
                            }
                        }
                    }, new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            mPresenter.userStatus(orderId, mData.get(position).getUserUid(), 1);
                            normalDialog.superDismiss();
                        }
                    });
                    normalDialog.show();
                }
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.getUserList(pageIndex, pageSize, userUid, orderId, 0);
    }
}
